
package com.innovamed.websocket.plugin;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.innovamed.InnovamedWsAppApplication;
import com.innovamed.websocket.constants.RabbitMqConstants;
import com.innovamed.websocket.service.MyConsumer;
import com.innovamed.websocket.utils.queue.RoutingKeyGenerator;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


/**
 * Copyright: Copyright (c) Jun_Zhou @PackageName:com.innovamed.websocket.plugin @Description:
 * Ws服务的插件集合类;
 *
 * @version: v1.0.0
 * @author: JunZhou @Email: 1769676159@qq.com @Site: CERNO
 * @date: 2019/8/24 20:59
 */
@Component
public class AppPlugin
{
	@Autowired
	private MyConsumer			myConsumer;
	
	@Value("${spring.rabbitmq.host}")
	private String				rabbitMQHost;
	
	@Value("${spring.rabbitmq.port}")
	private String				rabbitMQPort;
	
	private static AppPlugin	APP_PLUGIN_INS_HOLDER;
	
	@PostConstruct
	public void initInsAttribute()
	{
		APP_PLUGIN_INS_HOLDER = this;
	}
	
	private static final Logger log = LoggerFactory.getLogger(InnovamedWsAppApplication.class);
	
	public static void initRabbitMQCustomer() throws IOException, TimeoutException
	{
		Connection connection = getRabbitMQConnection();
		
		// 4、 通过Connection创建一个新的defaultChanne
		Channel defaultChanne = connection.createChannel();
				defaultChanne.exchangeDeclare(RabbitMqConstants.DEFAULT_EXCHANGE_NAME, RabbitMqConstants.DEFAULT_EXCHANGE_TYPE, true, false, null);
				defaultChanne.queueDeclare(RabbitMqConstants.DEFAULT_QUEUE_NAME, true, false, false, null);
				defaultChanne.queueBind(RabbitMqConstants.DEFAULT_QUEUE_NAME, RabbitMqConstants.DEFAULT_EXCHANGE_NAME, RabbitMqConstants.DEFAULT_ROUTING_KEY);
		
		// 5、 使用自定义消费者
				APP_PLUGIN_INS_HOLDER.myConsumer.setChannel(defaultChanne);
				defaultChanne.basicConsume(RabbitMqConstants.DEFAULT_QUEUE_NAME, false, APP_PLUGIN_INS_HOLDER.myConsumer);
		log.info("集群消费端启动成功");
	}

	public static void destoryRabbitMQCustomer() throws IOException, TimeoutException
	{
		Connection connection = getRabbitMQConnection();
		
		// 4、 通过Connection创建一个新的defaultChanne
		Channel defaultChanne = connection.createChannel();
				defaultChanne.exchangeDelete(RabbitMqConstants.DEFAULT_EXCHANGE_NAME);
				defaultChanne.queueDelete(RabbitMqConstants.DEFAULT_QUEUE_NAME);
				connection.close();
		log.info("集群消费端销毁成功");
	}
	
	/**
	   *  获取rabbitMQ服务端的连接;
	 * @return
	 * @throws IOException
	 * @throws TimeoutException
	 */
	public static Connection getRabbitMQConnection() throws IOException, TimeoutException
	{
		// 1、初始化RabbitMQ配置信息中的变量;
		initRabbitMQConfigVariable();
		
		// 2、创建ConnectionFactory
		ConnectionFactory connectionFactory = new ConnectionFactory();
						  connectionFactory.setHost(RabbitMqConstants.RABBITMQ_HOST);
						  connectionFactory.setPort(RabbitMqConstants.RABBITMQ_PORT);
						  connectionFactory.setVirtualHost(RabbitMqConstants.RABBITMQ_DEFAULT_VIRTUAL_HOST);
		
		// 3、 获取C onnection
		Connection connection = connectionFactory.newConnection();
		return connection;
	}
	
	/** 初始化RabbitMQ配置信息中的变量; */
	public static void initRabbitMQConfigVariable()
	{
		String rabbitMQHost   =  APP_PLUGIN_INS_HOLDER.rabbitMQHost;
		String rabbitMQPort   =  APP_PLUGIN_INS_HOLDER.rabbitMQPort;
						       	 RabbitMqConstants.RABBITMQ_PORT = Integer.parseInt(rabbitMQPort);
						         RabbitMqConstants.RABBITMQ_HOST = rabbitMQHost;
		String defaultRouteKey = RoutingKeyGenerator.generateDefaultRouteKey();
							     RabbitMqConstants.DEFAULT_EXCHANGE_NAME = defaultRouteKey;
							     RabbitMqConstants.DEFAULT_ROUTING_KEY = defaultRouteKey;
							     RabbitMqConstants.DEFAULT_QUEUE_NAME = defaultRouteKey;
		log.info("-----route_key---->" + RabbitMqConstants.DEFAULT_ROUTING_KEY);
	}
}
